<template>
  <el-drawer class="drawer-container" :visible.sync="visible" title="授权用户" append-to-body size="800px">
    <div class="drawer-search">
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入用户名称" @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="queryParams.mobile" clearable placeholder="请输入手机号码" @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="drawer-content">
      <div class="drawer-operate">
        <el-button icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd">添加</el-button>
        <el-button :disabled="multiple" icon="el-icon-circle-close" plain size="mini" type="danger"
                   @click="handleDeleteBatch">取消授权
        </el-button>
      </div>
      <div class="drawer-data">
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column align="center" type="selection" width="55"/>
          <el-table-column :show-overflow-tooltip="true" label="用户名称" prop="name"/>
          <el-table-column :show-overflow-tooltip="true" label="用户编号" prop="code"/>
          <el-table-column :show-overflow-tooltip="true" label="邮箱" prop="email"/>
          <el-table-column :show-overflow-tooltip="true" label="手机" prop="mobile"/>
          <el-table-column align="center" label="状态" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="localDict.user_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button v-has-permi="['system:role:remove']" icon="el-icon-circle-close" size="mini" type="text"
                         @click="handleDelete(scope.row)">取消授权
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="pageParam.total>0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                    :total="pageParam.total" @pagination="getList"/>
      </div>
    </div>
    <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery"/>
  </el-drawer>
</template>

<script>
import {allocatedUserList} from "@/api/system/role";
import selectUser from "./SelectUserCard.vue";
import {getStaticData} from "@/api/common";
import {remove} from "../../../../api/base";

export default {
  name: "AuthCard",
  components: {selectUser},
  props: {
    roleId: String,
  },
  data() {
    return {
      // 选中用户组
      ids: [],
      // 非多个禁用
      multiple: true,
      visible: false,
      // 遮罩层
      loading: true,
      baseUrl: '/system/user/role/',
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      },
      localDict: {
        user_status: []
      }
    };
  },
  created() {
    this.getStaticDict()
    this.resetQuery()
  },
  methods: {
    async getStaticDict() {
      await getStaticData('system-status').then(res => {
        this.localDict.user_status = res.data
      })
    },
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedUserList(this.roleId, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
          this.dataList = res.rows;
          this.pageParam.total = res.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    handleAdd() {
      this.$refs.select.show();
    },
    /** 取消授权按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('确认要取消该用户"' + row.name + '"角色吗？').then(function () {
        remove(this.baseUrl, row.id, false).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        }).catch(() => {
        });
      })
    },
    /** 批量取消授权按钮操作 */
    handleDeleteBatch() {
      this.$modal.confirm('是否取消选中用户授权数据项？').then(() => {
        remove(this.baseUrl, this.ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        }).catch(() => {
        })
      });
    },
  }
};
</script>
